package cn.xclink.kernel.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xclink.common.config.AppConfig;
import cn.xclink.common.web.BaseController;
import cn.xclink.common.web.ExecuteResult;
import cn.xclink.common.web.bind.annotation.CurrentUser;
import cn.xclink.kernel.entity.Org;
import cn.xclink.kernel.entity.User;
import cn.xclink.kernel.service.OrgService;

@Controller
@RequestMapping("system/org")
public class OrgController extends BaseController<Org>{
	
	public static String LEFT_DIVID = "sys_org_left_div";
	public static String RIGHT_DIVID = "sys_org_right_div";
	
	@Autowired
	private OrgService orgService;
	
	@Autowired
	private AppConfig config;
	
	@ModelAttribute
	public void init(Model model) {
		model.addAttribute("left_divid", LEFT_DIVID);
		model.addAttribute("right_divid", RIGHT_DIVID);
	}
	
	@RequiresPermissions("org:view")
	@RequestMapping
	public String index(@CurrentUser User currentUser, Model model) {
		//List<Org> orgList = orgService.findOrgByUserId(currentUser.getUserId());
		List<Org> orgList = orgService.findAll();
		model.addAttribute("orgList", orgList);
		return "system/org/index";
	}
	
	@RequiresPermissions("org:view")
	@RequestMapping(value = "/list")
	public String list(@CurrentUser User currentUser, Org org, Model model,HttpServletRequest request,	
			@RequestParam(value="pageNum",required=false) Integer pageNum, 
    		@RequestParam(value="pageSize",required=false) Integer pageSize) {
		
//		List<Org> orgList = orgService.findOrgByUserIdAndParentIds(currentUser.getUserId(),org.getParentIds());
//		if(orgList.size() == 0){
//			model.addAttribute("pageInfo", new PageInfo<User>(new ArrayList<User>()));
//		}else{
//			//org.setOrgIds(orgService.findOrgIds(orgList));
//			//model.addAttribute("pageInfo", orgService.findPageInfo(org, pageNum, pageSize));
//		}
		model.addAttribute("pageInfo", orgService.findPageInfo(org, pageNum, pageSize));
		model.addAttribute("org", org);
		return "system/org/list";
	}
	
	@RequiresPermissions("org:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String createForm(@CurrentUser User currentUser,Org org, Model model,HttpServletRequest request) {
		Org parent = orgService.get(org.getParentId());
		model.addAttribute("parent", parent);
		org.setParentIds(parent.makeSelfAsParentIds());
		model.addAttribute("org", org);
		return "system/org/create";
	}
	
	@RequiresPermissions("org:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(Org org) {
		try {
			Integer result = orgService.save(org);
			if (result == 1) {
				return  ExecuteResult.jsonReturn(200,LEFT_DIVID + ","+ RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}
	
	@RequiresPermissions("org:edit")
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String editForm(@PathVariable("id") Long id, Model model) {
		Org org  = orgService.get(id);
		model.addAttribute("org", org);
		Org parent = orgService.get(org.getParentId());
		model.addAttribute("parent", parent);
		return "system/org/edit";
	}

	@RequiresPermissions("org:edit")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(Org org) {
		try {
			Integer result = orgService.updateNotNull(org);
			if (result == 1) {
				return ExecuteResult.jsonReturn(200,LEFT_DIVID + ","+ RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}

	@RequiresPermissions("resource:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@PathVariable("id") Long id,HttpServletRequest request) {
		try {
			Integer result = orgService.delete(id);
			if (result == 1) {
				return ExecuteResult.jsonReturn(200,LEFT_DIVID + ","+ RIGHT_DIVID);
			} else {
				return ExecuteResult.jsonReturn(300, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ExecuteResult.jsonReturn(300, e.getMessage(),false);
		}
	}
}
